import java.util.ArrayList;
import java.util.Iterator;

import junit.framework.TestCase;


public class SmartRobotTest extends TestCase {
	
	private ArrayList<SmartRobot> robots;
	private RV1 rv1;
	
	public void setUp(){
		robots = new ArrayList<SmartRobot>();
		addRobot(new RV1());
		addRobot(new RV2());
		addRobot(new Aibo());
		
		rv1 = new RV1();
		
	}
	//add robot to list
	private void addRobot(AbstractRobot r){
		robots.add(new SmartRobot(r));
	}
	
	public void testDirection1() {
		//test direction - same for all robots
		SmartRobot r1 = robots.get(0), r2 = robots.get(1), r3 = robots.get(2);
		r1.move(10, -20);
		assertEquals(r1.direction, SmartRobot.Direction.LEFT);
		
		r2.move(10, 70);
		assertEquals(r2.direction, SmartRobot.Direction.RIGHT);
		
		r3.move(10,180);
		assertEquals(r3.direction, SmartRobot.Direction.RIGHT);
		
	}
	
	//tests robots moving one step
	public void testMove() {
		
		//move robots
		Iterator<SmartRobot> it = robots.iterator();
		while(it.hasNext()){
			it.next().move(30, 50);
		}
		//act once
		Iterator<SmartRobot> it2 = robots.iterator();
		while(it2.hasNext()){
			it2.next().act();
		}
		//check if moved to correct position
		Iterator<SmartRobot> it3 = robots.iterator();
		assertEquals(it3.next().getCurrentPosition().isSame(new Position(3,9)),true);	//RV1
		assertEquals(it3.next().getCurrentPosition().isSame(new Position(13,15)),true); //RV2
		assertEquals(it3.next().getCurrentPosition().isSame(new Position(23,19)),true); //AIBO
		
	}
	
	//tests heading directions
	public void testHeading(){
		
		//move robots
		Iterator<SmartRobot> it = robots.iterator();
		while(it.hasNext()){
			it.next().move(20, -150);
		}
		//move regular rv1
		rv1.move(20, -150);
		
		//act 5 times
		for (int i = 0; i < 5; i++) {
			Iterator<SmartRobot> it2 = robots.iterator();
			while(it2.hasNext()){
				it2.next().act();
			}
			rv1.act();
		}
		
		//check if moved to correct position
		Iterator<SmartRobot> it3 = robots.iterator();
		assertEquals(it3.next().getHeading(), 260);	//RV1
		assertEquals(it3.next().getHeading(), 210);	//RV2
		assertEquals(it3.next().getHeading(), 210);	//AIBO
		
		assertEquals(rv1.getHeading(), -20); //regular rv1
	
	}
	
	//tests smart robot - moving a few steps
	public void testAct() {
		
		//move robots
		Iterator<SmartRobot> it = robots.iterator();
		while(it.hasNext()){
			it.next().move(30, 50);
		}
		//move regular rv1
		rv1.move(30, 50);
		
		//act 5 times
		for (int i = 0; i < 5; i++) {
			Iterator<SmartRobot> it2 = robots.iterator();
			while(it2.hasNext()){
				it2.next().act();
			}
			rv1.act();
		}
		
		//check if moved to correct position
		Iterator<SmartRobot> it3 = robots.iterator();
		assertEquals(it3.next().getCurrentPosition().isSame(new Position(33,35)),true);	//RV1
		assertEquals(it3.next().getCurrentPosition().isSame(new Position(73,67)),true);	//RV2
		assertEquals(it3.next().getCurrentPosition().isSame(new Position(115,95)),true);//AIBO
	
		assertEquals(rv1.getCurrentPosition().isSame(new Position(15,45)), true);
	}

}
